class Thing < ActiveRecord::Base

  #To support chinese index
  GENERIC_ANALYSIS_REGEX = /([a-zA-Z]|[\xc0-\xdf][\x80-\xbf])+|[0-9]+|[\xe0-\xef][\x80-\xbf][\x80-\xbf]/
  GENERIC_ANALYZER = Ferret::Analysis::RegExpAnalyzer.new(GENERIC_ANALYSIS_REGEX, true)
  acts_as_ferret({:fields => [ :title, :tags_with_spaces ] }, { :analyzer => GENERIC_ANALYZER })
  #To support taggable
  acts_as_taggable

  acts_as_commentable
  
  has_many :user_things, :dependent =>:nullify  
  has_many :users, :through  => :user_things
  
  def users_things
    logger.debug "======the thing id=#{id}"
    @users_things = UsersThings.find(:all, :conditions =>  ["thing_id = ?", id] )
  end
  
  def Thing.isExisted?(pTitle)
    logger.debug "======>is there an existed thing '#{pTitle}' already"
    t = Thing.find(:first, :conditions => ["title = ?", pTitle])
    !t.nil?
  end
  
  def Thing.find_by_title(pTitle)
    logger.debug "======>find the existed thing by title '#{pTitle}"
    
    @the_first_thing = Thing.find(:first, :conditions => ["title = ?", pTitle])
  end
  
  # delimitor
  def tags_with_spaces
    return self.tag_names.join(" ")
  end

  # define the display order by the user count
  def <=>(other)
    self.user_count <=> other.user_count
  end  
end
